$(function(){
	initHbasesetting();
});
/**
 * 初始化hbase设置
 */
function initHbasesetting() {
	var code = getHbases();
	if (code == 0) {
		getHbaseProperties();
	}
}
/**
 * 获得hbase的连接配置
 */
function getHbaseProperties() {
	var hbaseselectele = $("#hbaseselect");
	getHbasePro(hbaseselectele);
	var code2 = getTables();
	if (code2 == 0) {
		$("#rowkeyrules").html(getRowkeys());
	}
}
/**
 * 获得rowkey配置并生成html
 */
function getSettingRowkeys() {
	$("#rowkeyrules").html(getRowkeys());
}

/**
 * 获得额外的rowkey配置
 * @param element
 */
function getextra(element){
	var select = element.value;
	var extra = "";
	if (select == 'string') {
		
	}
	else if (select == 'time') {
		extra = "<section class=\"pull-left no-padding col pull-width120\">"+
						"<label class=\"\">时间格式</label>"+
						"<label class=\"input\"><input  type=\"text\"  class=\"input-sm\" ></label>"+
						"</section>";
	}
	else if (select == 'timestamp') {
		extra = "<section class=\"pull-left no-padding col pull-width60\">"+
		"<label class=\"\">时间戳长度</label>"+
		"<label class=\"input\"><select class=\"input-sm\"><option selected=\"selected\" value=\"13\">13</option><option value=\"10\">10</option></select></label>"+
		"</section>";
	}
	var extraele = element.parentNode.parentNode.nextSibling;
	extraele.innerHTML = extra;
}
/**
 * 更新rowkey配置
 */
function updaterowkeys() {
	var tid = $("#htableselect").find("option:selected").val();
	var rowkeyrules = "[";
	var ele = $("#rowkeyrules").find("td > div");
	var isnameempty = false;
	var namestrarr = new Array(ele.length);
	var isnamerepeat = false;
	for (var i = 0; i < ele.length; i++) {
		var sections = ele.eq(i).children('section');
		var namevalue = sections.eq(0).find("input").eq(0).val();
		if (namevalue == null || namevalue.replaceAll(" ","") == '') {
			isnameempty = true;
			break;
		}
		else {
			if (namestrarr.contains(namevalue)) {
				isnamerepeat = true;
				break;
			}
			else {
				namestrarr[i] = namevalue;
			}
		}
		
	}
	if (isnameempty) {
		dialog('error','name不能为空',3000);
	}
	else if (isnamerepeat) {
		dialog('error','name不能重复',3000);
	}
	else {
		for (var i = 0; i < ele.length; i++) {
			var sections = ele.eq(i).children('section');
			var rowkeyrule="{";
			for (var j = 0; j < sections.length; j++) {
				var key;
				var value = "";
				if (j == 1) {
					key = "type";
					value = sections.eq(j).find("select").eq(0).find("option:selected").val();
				}
				//是否倒序
				else if (j == 2){
					key = "reverse";
					value = sections.eq(j).find("input").eq(0).is(':checked');
				}
				//是否自动补位
				else if (j == 4) {
					key = "autocover";					
					value = sections.eq(j).find("input").eq(0).is(':checked');
				}
				else if (j == 0) {
					key = "name";
					value = sections.eq(j).find("input").eq(0).val();
				}
				//是否MD5转码
				else if (j == 3) {
					key = "usemd5";
					value = sections.eq(j).find("input").eq(0).is(':checked');
				}
				//固定值
				else if (j == 5) {
					key = "value";
					value = sections.eq(j).find("input").eq(0).val();
				}
				if (rowkeyrule=="{") {
					rowkeyrule+="\""+key+"\":\""+value+"\"";
				}
				else {
					rowkeyrule+=",\""+key+"\":\""+value+"\"";
				}	
			}
			var sectionsextra = ele.eq(i).children('div').eq(0);
			if (sectionsextra.find("select").length == 1){
				rowkeyrule+=",\"stamplen\":\""+sectionsextra.find("select").eq(0).find("option:selected").val()+"\"";
			}
			else if (sectionsextra.find("input").length == 1) {
				rowkeyrule+=",\"format\":\""+sectionsextra.find("input").eq(0).val()+"\"";
			}
			var sectionsautocover = ele.eq(i).children('div').eq(1).children('section');
			for (var k = 0; k < sectionsautocover.length; k++) {
				var key;
				var value = "";
				if (k == 1) {
					//自动补位的位置，是在前还是后
					key = "autocoverLoc";
					value = sectionsautocover.eq(k).find("select").eq(0).find("option:selected").val();
				}
				else if (k == 2){
					//自动补位什么内容
					key = "autocoverMark";
					value = sectionsautocover.eq(k).find("input").eq(0).val();
				}
				else if (k == 0) {
					//自动补位的长度
					key = "autocoverLen";
					value = sectionsautocover.eq(k).find("input").eq(0).val();
				}
				rowkeyrule+=",\""+key+"\":\""+value+"\"";
			}
			rowkeyrule+="}"
			rowkeyrules+=rowkeyrule;	
		}
		rowkeyrules+="]";
		$.ajax({
			url : "/smarttest-frontstage/dataprovider/updateRowkeyrule.action",
			data : {tid:tid,rowkeyrule:rowkeyrules},
			method : 'post',
			async:false,
			error:function(response,error){
				dialog('error','error'+response.status,3000);
				}, 
			success : function(data) {
				if (data.data <= 0) {
					dialog('notice','提交成功,无数据更新',3000);
				}
				else {
					dialog('succ','提交成功',3000);
				}
			}
		});
	}
}
/**
 * 更新hbase连接配置
 */
function updatehbasepro() {
	var hid = $("#hbaseselect").find("option:selected").val();
	var rootdir = $("#rootdir").val();
	var znodeparent = $("#znodeparent").val();
	var quorum = $("#quorum").val();
	var usehostnameonly = $("#usehostnameonly").val();
	$.ajax({
		url : "/smarttest-frontstage/dataprovider/updatehbasepro.action",
		data : {hid:hid,rootdir:rootdir,znodeparent:znodeparent,quorum:quorum,usehostnameonly:usehostnameonly},
		method : 'post',
		async:false,
		error:function(response,error){
			dialog('error','error'+response.status,3000);
			}, 
		success : function(data) {
			if (data.data <= 0) {
				dialog('notice','提交成功,无数据更新',3000);
			}
			else {
				dialog('succ','提交成功',3000);
			}
		}
	});
}
/**
 * 添加rowkey
 */
function addrowkey() {
	var addhtml = "<tr><td style=\"text-align: left; vertical-align: middle;\">" +
					"<div style=\"clear:both;\">"+
					"<section class=\"pull-left no-padding col pull-width120\" style=\"margin: 2px\"><label class=\"\">name</label><label class=\"input\"><input  class=\"input-sm\" type=\"text\" value=\"\" placeholder=\"\" /></label></section>"+
					"<section class=\"pull-left no-padding col pull-width80\" style=\"margin: 2px\"><label class=\"\">类型</label><label class=\"input\"><select  class=\"input-sm\" onchange=\"getextra(this);\"><option selected=\"selected\" value=\"string\">string</option><option value=\"time\">time</option><option value=\"timestamp\">timestamp</option></select></label></section>"+
					"<div style=\"float:left\"/>"+
					"<section class=\"pull-left no-padding col pull-width40\" style=\"margin: 2px\"><label class=\"\">倒序</label><label class=\"checkbox\"><input  type=\"checkbox\"/><i></i></label></section>"+
					"<section class=\"pull-left no-padding col pull-width40\" style=\"margin: 2px\"><label class=\"\">MD5(32)</label><label class=\"checkbox\"><input type=\"checkbox\"/><i></i></label></section>"+
					"<section class=\"pull-left no-padding col pull-width60\" style=\"margin: 2px\"><label class=\"\">自动补位</label><label class=\"checkbox\"><input type=\"checkbox\" onclick=\"autocover(this);\"/><i></i></label></section>"+
					"<div style=\"float:left\"/>"+
					"<section class=\"pull-left no-padding col pull-width120\" style=\"margin: 2px\"><label class=\"\">使用固定值</label><label class=\"input\"><input  class=\"input-sm\" type=\"text\" placeholder=\"\" /></label></section>"+
					"<a href=\"####\" onclick=\"deleterowkey(this);\"><img border=\"0\" src=\"img/delete.gif\" width=\"20\" height=\"20\"></a>"+
					"</div>"+
					"</td></tr>";
	$("#rowkeyrules").append(addhtml);
}
/**
 * 添加hbase
 */
function addhbase () {
	var name = $("#hbasename").val();
	var rootdir = $("#rootdir_modal").val();
	var znodeparent = $("#znodeparent_modal").val();
	var quorum = $("#quorum_modal").val();
	var usehostnameonly = $("#usehostnameonly_modal").val();
	if (name == null || name.replaceAll(" ","") == '') {
		dialog('error','名称不能为空',3000);
	}
	else {
		$.ajax({
			url : "/smarttest-frontstage/dataprovider/getHbaseNames.action",
			method : 'GET',
			async:false,
			error:function(response,error){
				dialog('error','error'+response.status,3000);
				}, 
			success : function(data) {
				var names = data.data;
				if (names.contains(name)) {
					dialog('error','hbase名不能重复',3000);
				}
				else {
					$.ajax({
						url : "/smarttest-frontstage/dataprovider/addHbase.action",
						data : {name:name,rootdir:rootdir,znodeparent:znodeparent,quorum:quorum,usehostnameonly:usehostnameonly},
						method : 'POST',
						error:function(response,error){
							dialog('error','error'+response.status,3000);
							}, 
						success : function(data) {
							if (data.data >= 1) {
								$("#hbasename").val("");
								$("#rootdir_modal").val("");
								$("#znodeparent_modal").val("");
								$("#quorum_modal").val("");
								$("#usehostnameonly_modal").val("");
								dialog('succ','添加成功',3000);
								var code = getHbases();
								if (code == 0) {
									$("#hbaseselect option").each(function(){  
									        if($(this).text() == name){  
									            $(this).attr("selected","selected");  
									        }
									});  
									var interval = setInterval(function(){
										if ($("#hbaseselect").find("option:selected").text() == name) {
											getHbaseProperties();
											clearInterval(interval);
										}
									},10);
								}
							}
						}
					});
				}
			}
		});			
	}
}
/**
 * 添加htable
 */
function addhtable() {
	var hbaseid = $("#hbaseselect").find("option:selected").val();
	var htablename = $("#htablename").val();
	if (htablename == null || htablename.replaceAll(" ","") == '') {
		dialog('error','名称不能为空',3000);
	}
	else {
		$.ajax({
			url : "/smarttest-frontstage/dataprovider/getHtableNames.action",
			method : 'GET',
			data : {hid:hbaseid},
			async:false,
			error:function(response,error){
				dialog('error','error'+response.status,3000);
				}, 
			success : function(data) {
				var names = data.data;
				if (names.contains(htablename)) {
					dialog('error','表名不能重复',3000);
				}
				else {
					$.ajax({
						url : "/smarttest-frontstage/dataprovider/addHTable.action",
						data : {name:htablename,usemd5:0,hid:hbaseid},
						method : 'POST',
						error:function(response,error){
							dialog('error','error'+response.status,3000);
							}, 
						success : function(data) {
							if (data.data >= 1) {
								$("#htablename").val("");
								var code = getTables();
								if (code == 0) {
									$("#htableselect option").each(function(){  
								        if($(this).text() == htablename){  
								            $(this).attr("selected","selected");  
								        }
									});  
									var interval = setInterval(function(){
										if ($("#htableselect").find("option:selected").text() == htablename) {
											addRowkeyRule($("#htableselect").find("option:selected").val(),"");											
											$("#rowkeyrules").html(getRowkeys());
											dialog('succ','添加成功',3000);
											clearInterval(interval);
										}
									},10);
								}
							}
						}
					});
				}
			}
		});			
	}
}
/**
 * 添加rowkey规则
 * @param tid
 * @param rowkeyrule
 */
function addRowkeyRule(tid,rowkeyrule) {
	$.ajax({
		url : "/smarttest-frontstage/dataprovider/addRowkeyRule.action",
		data : {tid:tid,rowkeyrule:rowkeyrule},
		method : 'post',
		async:false,
		error:function(response,error){
			dialog('error','error'+response.status,3000);
			code2 = -1;
			}, 
		success : function(data) {
			if (data.data <= 0) {
				dialog('error','提交失败',3000);
				code2 = -1;
			}
			else {
				dialog('succ','提交成功',3000);
				code2 = 0;
			}
		}
	});
}
/**
 * 删除rowkey
 * @param element
 * @returns {Boolean}
 */
function deleterowkey(element) {
	var e = element.parentNode;
	var e2 = element.parentNode.parentNode;
	e2.removeChild(e);
	return false;
}
/**
 * 自动补位
 * @param element
 */
function autocover(element) {
	var cover= "";
	if(element.checked==true) {
		cover = "<section class=\"pull-left no-padding col pull-width40\" style=\"margin: 2px\">"+
		"<label class=\"\">位数</label>"+
		"<label class=\"input\">" +
		"<input type=\"text\" maxlength=\"2\" onkeyup=\"this.value=this.value.replace(/[^0-9]/g,'')\" onafterpaste=\"this.value=this.value.replace(/[^0-9]/g,'')\">"+
		"</label>"+
		"</section>"+
		"<section class=\"pull-left no-padding col pull-width40\" style=\"margin: 2px\">"+
		"<label class=\"\">位置</label>"+
		"<label class=\"input\">" +
		"<select class=\"input-sm\"><option value=\"before\" selected=\"selected\">前</option><option value=\"after\">后</option></select>"+
		"</label>"+
		"</section>"+
		"<section class=\"pull-left no-padding col pull-width40\" style=\"margin: 2px\">"+
		"<label class=\"\">符号</label>"+
		"<label class=\"input\"><input  type=\"text\"  class=\"input-sm\"></label>"+
		"</section>";
	}	
	var coverele = element.parentNode.parentNode.nextSibling;
	coverele.innerHTML = cover;
}